package cn.sicnu.cs.employment.service.impl;

import cn.sicnu.cs.employment.domain.entity.BackgroundComment;
import cn.sicnu.cs.employment.mapper.BackgroundCommentsMapper;
import cn.sicnu.cs.employment.service.IBackgroundCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class BackgroundCommentServiceImpl implements IBackgroundCommentService {

    private final BackgroundCommentsMapper commentsMapper;

    @Override
    public Set<BackgroundComment> getComments(Long empId, Long comId) {
        return commentsMapper.selectCommentsByComAndEmp(empId,comId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void insertCommment(BackgroundComment commentToSave) {
        commentsMapper.insert(commentToSave);
    }

    @Override
    public boolean isAvailable(Long id) {
        long create = commentsMapper.getCreateTime(id).getTime();
        long now = System.currentTimeMillis();
        long limit = 30 * 24 * 60 * 60 * 1000L;
        return now - create < limit  ;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void deleteInfoById(Long id) {
        commentsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void addSelfComment(String content, Long id) {
        commentsMapper.updateById(
                BackgroundComment.builder()
                .id(id)
                .self(content)
                .build()
        );
    }
}